﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability.APP.Application.Command.Breeds;
using Traceability.Damoin;
using Traceability.ErrorCode;
using Traceability.Infaseture;

namespace Traceability.APP.Application.CommandHadnLer.Breeds
{
    public class GetBreedCommandHandLer : IRequestHandler<GetBreedCommand, APIResult<List<Breed>>>
    {

        private readonly IBaseRepostiy<Breed> _breed;

        public GetBreedCommandHandLer(IBaseRepostiy<Breed> breed)
        {
            _breed = breed;
        }
        public async Task<APIResult<List<Breed>>> Handle(GetBreedCommand request, CancellationToken cancellationToken)
        {
            // 获取所有养殖信息
            var list =  _breed.GetAll().Where(x => !x.IsDeleted);

            // 根据批次筛选
            list = list.Where(x => x.Batch == request.Batch) ;

            // 按照 Id 降序排序
            list = list.OrderByDescending(x => x.Id);
            return new APIResult<List<Breed>>
            {
                Code = ResultCode.Ok,
                Data = list.ToList(),
                Message = "获取成功"
            };
        }
    }
}
